package com.applink.sell_proof.fragment;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.applink.sell_proof.ChangePassword;
import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.NewLogInActivity;
import com.applink.sell_proof.R;
import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.api.Methods;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.database.table.CustomerSupportHelper;
import com.applink.sell_proof.database.table.UserSettingDBHelper;
import com.applink.sell_proof.model.UserProfile;
import com.applink.sell_proof.model.UserSetting;

public class UserSettingFragment extends SherlockFragment implements Observer,
		OnClickListener {
	protected ViewGroup mLayout;
	private UserProfile mUserProfile;

	public static UserSettingFragment newInstance(long userID) {
		UserSettingFragment f = new UserSettingFragment();
		Bundle args = new Bundle();
		args.putLong(Constant.INTENT_EDIT_USERSETTING, userID);
		f.setArguments(args);
		return f;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.user_profile, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_sign_out) {
			EMarketing.getInstance().setExpiredLocal("");
//			EMarketing.getInstance().setAccountEmail("");
//			EMarketing.getInstance().setAccountPassword("");
			EMarketing.getInstance().setLogin(false);
			Intent intent = new Intent(getActivity(), NewLogInActivity.class);			
			getActivity().finish();
			 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
			startActivity(intent);
			return true;
		}
		return false;
	}	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mLayout == null) {
			mLayout = (ViewGroup) inflater.inflate(
					R.layout.custsvr_user_profile_new, null, false);
		} else {
			if (mLayout.getParent() != null) {
				((ViewGroup) mLayout.getParent()).removeView(mLayout);
			}
		}
		setHasOptionsMenu(true);
		SetUserSettingInfo();
		//mLayout.findViewById(R.id.btn_edit_delivery).setOnClickListener(this);
		mLayout.findViewById(R.id.btn_edit_office).setOnClickListener(this);
		mLayout.findViewById(R.id.btn_edit_profile).setOnClickListener(this);
		mLayout.findViewById(R.id.btn_change_pass).setOnClickListener(this);

		// initial default tab

		Spinner spinnerTab = (Spinner) mLayout
				.findViewById(R.id.spinner_default_tab);
		String[] tabs = getResources().getStringArray(R.array.tab_name);
		ArrayAdapter<String> adapter_tab = new ArrayAdapter<String>(
				getActivity(), R.layout.spinner_item, tabs);
		spinnerTab.setAdapter(adapter_tab);
		int current_tab = CustomerSupportHelper.getInt(Constant.DEFAULT_TAB);
		spinnerTab.setSelection(current_tab);
		spinnerTab.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				CustomerSupportHelper.storeInt(Constant.DEFAULT_TAB, position);

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		return mLayout;
	}

	public void SetUserSettingInfo() {
		ArrayList<UserSetting> userSetting = UserSettingDBHelper.getAllUserSetting();
//		if (userSetting == null) {
			Log.e("GetCustomerInfo", "return value is null");
			DataHelper.getInstance().getUserSetting();
//			return;
//		} else {
//			BindDataToControl(userSetting.get(0));
//		}

	}

	private void BindDataToControl(UserSetting userSetting) {
		((TextView) mLayout.findViewById(R.id.textSalutation)).setText(userSetting.getSalutation());		
		((TextView) mLayout.findViewById(R.id.txtfirstname)).setText(userSetting.getFirstName());
		((TextView) mLayout.findViewById(R.id.txtlastname)).setText(userSetting.getLastName());
		((TextView) mLayout.findViewById(R.id.txt_userpro_mobile)).setText(userSetting.getMobile1());
		((TextView) mLayout.findViewById(R.id.textEmailContact)).setText(userSetting.getEmail1());
		((TextView) mLayout.findViewById(R.id.txtJobTitle)).setText(userSetting.getJobTitle());
		
		((TextView) mLayout.findViewById(R.id.usrprfaddCountry)).setText(userSetting.getPartnerCompName());
		((TextView) mLayout.findViewById(R.id.txt_country)).setText(userSetting.getCountry());
		((TextView) mLayout.findViewById(R.id.txt_address_1)).setText(userSetting.getPartnerCompAddress1());
		((TextView) mLayout.findViewById(R.id.txt_address_2)).setText(userSetting.getPartnerCompAddress2());
		((TextView) mLayout.findViewById(R.id.txt_address_3)).setText(userSetting.getPartnerCompAddress3());
		
		((TextView) mLayout.findViewById(R.id.txt_postal_code)).setText(userSetting.getPartnerCompPostCode());
		((TextView) mLayout.findViewById(R.id.txt_phone_company)).setText(userSetting.getPartnerCompOfficeNo1());
		((TextView) mLayout.findViewById(R.id.txt_fax)).setText(userSetting.getPartnerCompFax());
		
		((TextView) mLayout.findViewById(R.id.txtuseridVal)).setText(String.valueOf(userSetting.getWebLogin()));
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		if (data == null)
			return;
		DataResult result = (DataResult) data;
		if (result.getMethodName() == Methods.getCustomerInfor) {
			ArrayList<UserSetting> userSetting = UserSettingDBHelper.getAllUserSetting();
			//UserProfile entity = CustomerSupportHelper.GetUserProfile();
			if (userSetting != null) {
				BindDataToControl(userSetting.get(0));
			} else {
				Log.e(Constant.USER_SETTING_FRAGMENT, "no UserProfile");
			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		DataHelper.getInstance().addObserver(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_edit_profile:
			DialogFragment editProfileFragment = new EditUserProfile();
			editProfileFragment.show(getActivity().getSupportFragmentManager(),
					null);
			break;
		case R.id.btn_edit_office:
			DialogFragment editOfficeFragment = new EditOfficeProfile();
			editOfficeFragment.show(getActivity().getSupportFragmentManager(),
					null);
			break;
		case R.id.btn_edit_delivery:
			DialogFragment editDeliveryFragment = new EditDeliveryProfile();
			editDeliveryFragment.show(
					getActivity().getSupportFragmentManager(), null);
			break;
		case R.id.btn_change_pass:
//			Intent 
//			//			DialogFragment changePass = ChangePassword.getInstance(EMarketing
////					.getInstance().getAccountEmail());
////			changePass.show(getActivity().getSupportFragmentManager(),
////					ChangePassword.TAG);
//			break;
			Intent intentUserSetting = new Intent(getActivity(), ChangePassword.class);
			startActivity(intentUserSetting);
			break;
		default:
			break;
		}

	}

}
